package com.ymatou.c2clivebizer.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;
/**
*首页直播买手推荐前台接口Call
*FreeMaker Template自动生成代码
*/
public class HomePageCall extends YmatouBaseCall{
	private static String opurl ="search/homepage/";
	
	public HomePageCall() {
		super(EnvSetup.getSystemUrl("homePage"),opurl,"GET","PARAM");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	/**
	 * 得到接口返回状态结果
	 * @return String OK
	 * */
	public String getStatus() {
		String ok = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			ok = jo.getString("status");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ok;
	}
	
	
	/**
	 * 获取接口返回数据的记录
	 * */
	public JSONArray getHits(){
		JSONArray list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			//list=jsonsList.getJSONObject("hits");
			list = jsonsList.getJSONArray("hits");
			if(list.toString().equals("[]")){
				return null;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 得到接口请求时间
	 * @return String OK
	 * */
	public String getTook() {
		String took = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			took = jo.getString("took");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return took;
	}
	
	/**
	 * 得到接口请求时间
	 * @return String OK
	 * */
	public boolean getTimeout() {
		boolean timeout = true;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			timeout = jo.getBoolean("timeout");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return timeout;
	}
	
	/**
	 * 得到接口服务器ip
	 * @return String OK
	 * */
	public String getHostIp() {
		String hostip = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			hostip = jo.getString("hostip");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hostip;
	}
	
	/**
	 * 得到requestid
	 * @return String OK
	 * */
	public String getRequestId() {
		String requestid = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			requestid = jo.getString("requestid");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return requestid;
	}
	
	/*取接口返回中的某个值放入list中
	 * 
	 * */
	public List getIdArray(String p){
		List<Integer> list = new ArrayList<Integer>();
		JSONArray hitsAry = getHits();
		
		try {
		for(int i=1;i<=hitsAry.length();i++){
			
			String ids	= hitsAry.get(i-1).toString();
			JSONObject json = new JSONObject(ids);
			
			int id = json.getInt(p);
			list.add(id);
		
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
}